﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;

/// <summary>
/// 部门类主要记录的部门的相关信息以及对部门的相关操作
/// </summary>
public class Department
{   
    private string  _depName;
    private int _status;
    private string   _principalID;
    private string _brief;
    //访问属性
    public string DepName
    {
        set { _depName = value; }
        get { return _depName; }
 
    }
    public string PrincipaID
    {
        set { _principalID = value; }
        get { return _principalID; }
 
    }
    public int Status
    {
        set { _status = value; }
        get{return _status;}
    }
    public string Brief 
    {
        set { _brief = value; }
        get { return _brief; }
    }
  

	public Department()
	{
		
	}
    /// <summary>
    /// 带参构造器，实例化一个department对象
    /// </summary>
    /// <param name="depName">部门名称</param>
    /// <param name="principalID">主管人ID</param>
    /// <param name="brief">相关说明</param>
    public Department(string depName,string  principalID,string brief)
    {
        _depName = depName;
        _principalID = principalID;
        _brief = brief;
    }
    /// <summary>
    /// 增加一个部门
    /// </summary>
    /// <returns>增加成功返回真，否则假</returns>
    public bool AddDepartment()
    {
        string strSql = "insert into DEPARTMENT(DEPNAME,PRINCIPALID,STATUS,BRIEF)VALUES("
                        +"'"+this.DepName+"',"
                        +"'"+this.PrincipaID+"',"
                        +"'"+this.Status+"',"
                        +"'"+this.Brief+"')";
        DBOperation db = new DBOperation();
        return  db.ExcuteSql(strSql);
    }
    /// <summary>
    /// 删除部门，保留用户的方法
    /// </summary>
    /// <param name="depID">被删除的部门的ID</param>
    /// <param name="depLeftID">接受被删除部门的成员的部门ID</param>
    /// <returns>成功返回真，否则假</returns>
    public  static bool DelDepSaveUser(int depID ,int depLeftID)
    {
       bool seccess = false;
       DBOperation db = new DBOperation();
       string strSqlDelDep = "delete DEPARTMENT where DEPID=" + depID + "";           //删除部门
       string strSqlSearchUser = "select USERNAME from USERS where DEPID=" + depID + "";  //提取被删除部门下的所有用户,并把这些用户放到一个动态数组中
       ArrayList strSqls = new ArrayList();
       SqlDataReader sdr = db.GetDataReader(strSqlSearchUser);
        while(sdr.Read())
        {
            strSqls.Add("update USERS set DEPID="+depLeftID+" where USERNAME='"+Convert.ToString( sdr[0])+"'");
        }
       strSqls.Add(strSqlDelDep);
       seccess = db.ExcuteSqls(strSqls);
       sdr.Close();
       return seccess;
       

    }
    /// <summary>
    /// 删除部门以及该部门下的用户，但是保留这些用户发送的信息
    /// </summary>
    /// <param name="depID">被删除的部门ID</param>
    /// <returns></returns>
   public  static bool DelDepDelUserSaveMsg(int depID)
    {
        bool seccess = false;
        DBOperation db = new DBOperation();
        
        //删除部门
        string strSqlDelDep = "delete DEPARTMENT where DEPID=" + depID + "";
        //删除用户
        string strSqlDelUser = "delete USERS where DEPID=" + depID + "";
        ArrayList strSqls = new ArrayList();
        strSqls.Add(strSqlDelDep);
        strSqls.Add(strSqlDelUser);
        seccess = db.ExcuteSqls(strSqls);
        return seccess;

    }
    /// <summary>
    /// 删除该部门下的所有信息，包括部门、用户、这些用户发送出的信息
    /// </summary>
    /// <param name="depID"></param>
    /// <returns></returns>
    public  static bool DelAll(int depID)
    {
        bool seccess = false;
        DBOperation db = new DBOperation();
        string strTableName = "MESSAGE" + Convert.ToString(DateTime.Now.Year);
        //删除部门，用户的sql语句
        string strDelDep = "delete DEPARTMENT where DEPID=" + depID + "";
        string strSqlDelUser = "delete USERS where DEPID=" + depID + "";
        //读取用户名，并把这些用户所发的信息都删除掉
        ArrayList strSqls=new ArrayList();
        string strSqlSearchUser = "select USERNAME from USERS where DEPID=" + depID + "";
        SqlDataReader sdr = db.GetDataReader(strSqlSearchUser);
        //构造删除信息的sql语句组
        while (sdr.Read())
        {
            strSqls.Add("delete "+strTableName+" where FROMUSERNAME='"+Convert.ToString(sdr[0])+"'");
        }
        strSqls.Add(strDelDep);
        strSqls.Add(strSqlDelUser);
        seccess=db.ExcuteSqls(strSqls);
        sdr.Close();
        return seccess;
        
    }
   
    /// <summary>
    /// 修改部门信息
    /// </summary>
    /// <param name="depID">需要修改的部门的ID</param>
    /// <returns>成功返回真，否则返回假</returns>
    public bool AlterDepartment(int depID)
    {
        string strAlterDep = "update DEPARTMENT set "
            + "DEPNAME='"+this.DepName+"',"
            + "PRINCIPALID='"+this.PrincipaID+"',"
            +"STATUS='"+this.Status+"',"
            + "BRIEF='" + this.Brief + "' where DEPID="+depID;
        DBOperation DB = new DBOperation();
        return DB.ExcuteSql(strAlterDep);
    }
    /// <summary>
    /// 根据部门名称，查找一个部门的信息，并将信息记录在一个dataRow返回
    /// </summary>
    /// <param name="depName">部门名称</param>
    /// <returns>记录着部门信息的dataRow</returns>
    public static  DataRow searchDep(string depName)
    {
        string strSearchDep = "select * from DEPARTMENT where DEPNAME='"+depName+"'";
        DBOperation db = new DBOperation();
        return  db.GetDateRow(strSearchDep);
     }
    /// <summary>
    /// 返回系统所有的部门信息
    /// </summary>
    /// <returns>返回dataSet</returns>
    public DataSet  FindDep()
    {
        DataSet ds = null;
        string strFindDep = "select * from DEPARTMENT ";
        String tableName = "department";
        DBOperation db = new DBOperation();
        ds = db.GetDataSet(strFindDep,tableName);
        return ds;
    }
 
}
